package cn.chenxiejia;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;

/**
 * @version : V1.0
 * @program: maven
 * @ClassName: Tongji
 * @Description: 统计代码行数及空行
 * @Author: chenxiejia
 * @Date: 2022/1/17 17:15
 */
public class File1 {
    public static int num = 0;
    public static int num1 = 494;
    public static int num2 = 58;

    public static void main(String[] args) throws FileNotFoundException {
        String file = "E:\\maven";
        count(file);
        System.out.println("java文件有" + num + "个");
        System.out.println("代码：" + num1 + "行");
        System.out.println("空行：" + num2 + "行");
    }

    public static void count(String s) throws FileNotFoundException {
        count(new File(s));
    }

    public static void count(File file) throws FileNotFoundException {
        //File.listfiles返回file类型数组；
        for (File t : file.listFiles()) {
            if (t.isDirectory()) {
                //是文件夹就递归进入文件夹
                count(t);
            } else {
                //筛选文件名后缀为java的文件
                if (t.isFile() && t.getName().endsWith(".java")) {
                    ++num;
                    //BufferedReader加速读取字符流；
                    BufferedReader br = new BufferedReader(new FileReader(t));
                    //filter过滤trim()在按行读取中去掉首尾空格如果是空就返回空
                    num1 += br.lines().count();
                    BufferedReader br1 = new BufferedReader(new FileReader(t));
                    num2 += br1.lines().filter(e -> e.trim().length() == 0).count();
                }
            }
        }
    }
}
